{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test McpExecutor Run Method\n",
    "\n",
    "This notebook tests the `run` method of the `McpExecutor` class from `autocoder.common.mcp_tools`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import json\n",
    "import asyncio\n",
    "import tempfile\n",
    "from pathlib import Path\n",
    "\n",
    "from autocoder.common.mcp_hub import McpHub, McpServer, McpTool, McpResource, McpResourceTemplate\n",
    "from autocoder.common.mcp_tools import McpExecutor\n",
    "import byzerllm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Helper function for async tests\n",
    "async def run_test(test_func):\n",
    "    \"\"\"Helper to run async test functions\"\"\"\n",
    "    try:\n",
    "        await test_func()\n",
    "        print(\"✅ Test passed\")\n",
    "    except AssertionError as e:\n",
    "        print(f\"❌ Test failed: {str(e)}\")\n",
    "    except Exception as e:\n",
    "        print(f\"❌ Test error: {str(e)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "async def test_mcp_executor_run():\n",
    "    \"\"\"Test McpExecutor run method\"\"\"\n",
    "    # Create settings file with filesystem server config\n",
    "    with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:\n",
    "        settings = {\n",
    "            \"mcpServers\": {\n",
    "                \"filesystem\": {\n",
    "                    \"command\": \"npx\",\n",
    "                    \"args\": [\n",
    "                        \"-y\",\n",
    "                        \"@modelcontextprotocol/server-filesystem\",\n",
    "                        \"/Users/allwefantasy/projects/tests\"                        \n",
    "                    ]\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "        json.dump(settings, f)\n",
    "        settings_path = f.name\n",
    "    \n",
    "    try:\n",
    "        # Initialize hub and executor\n",
    "        hub = McpHub()\n",
    "        # await hub.initialize()\n",
    "        \n",
    "        llm = byzerllm.ByzerLLM.from_default_model(model=\"deepseek_chat\")\n",
    "        executor = McpExecutor(hub, llm)\n",
    "        \n",
    "        # Create test conversation\n",
    "        conversations = [{\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"List the contents of /Users/allwefantasy/projects/tests\"\n",
    "        }]\n",
    "        \n",
    "        # Run the executor\n",
    "        final_conversations,results = await executor.run(conversations)        \n",
    "        # print(json.dumps(final_conversations, indent=2,ensure_ascii=False))\n",
    "        print(results)\n",
    "               \n",
    "        \n",
    "    finally:\n",
    "        # Cleanup\n",
    "        # await hub.shutdown()\n",
    "        os.unlink(settings_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2025-01-02 22:33:54.602\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2025-01-02 22:33:54,638\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2025-01-02 22:33:54,638\tINFO worker.py:1582 -- Calling ray.init() again after it has already been called.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Testing McpExecutor run method:\n",
      "[CallToolResult(content=[TextContent(type='text', text='[FILE] a.txt')], isError=False)]\n"
     ]
    }
   ],
   "source": [
    "# Run the test\n",
    "print(\"Testing McpExecutor run method:\")\n",
    "await test_mcp_executor_run()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "byzerllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
